package com.timmy.test1.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.LogUtils;
import com.timmy.test1.R;
import com.timmy.test1.common.CallBackMode;
import com.timmy.test1.common.Direction;
import com.timmy.test1.common.DirectionMode;

/**
 * Created by timmy on 2020/5/6.
 */

public class RockerView extends View {
    private static final double ANGLE_0 = 0.0D;

    private static final double ANGLE_360 = 360.0D;

    private static final double ANGLE_4D_OF_0P = 0.0D;

    private static final double ANGLE_4D_OF_1P = 90.0D;

    private static final double ANGLE_4D_OF_2P = 180.0D;

    private static final double ANGLE_4D_OF_3P = 270.0D;

    private static final double ANGLE_8D_OF_0P = 22.5D;

    private static final double ANGLE_8D_OF_1P = 67.5D;

    private static final double ANGLE_8D_OF_2P = 112.5D;

    private static final double ANGLE_8D_OF_3P = 157.5D;

    private static final double ANGLE_8D_OF_4P = 202.5D;

    private static final double ANGLE_8D_OF_5P = 247.5D;

    private static final double ANGLE_8D_OF_6P = 292.5D;

    private static final double ANGLE_8D_OF_7P = 337.5D;

    private static final double ANGLE_HORIZONTAL_2D_OF_0P = 90.0D;

    private static final double ANGLE_HORIZONTAL_2D_OF_1P = 270.0D;

    private static final double ANGLE_ROTATE45_4D_OF_0P = 45.0D;

    private static final double ANGLE_ROTATE45_4D_OF_1P = 135.0D;

    private static final double ANGLE_ROTATE45_4D_OF_2P = 225.0D;

    private static final double ANGLE_ROTATE45_4D_OF_3P = 315.0D;

    private static final double ANGLE_VERTICAL_2D_OF_0P = 0.0D;

    private static final double ANGLE_VERTICAL_2D_OF_1P = 180.0D;

    private static final int AREA_BACKGROUND_MODE_COLOR = 1;

    private static final int AREA_BACKGROUND_MODE_DEFAULT = 3;

    private static final int AREA_BACKGROUND_MODE_PIC = 0;

    private static final int AREA_BACKGROUND_MODE_XML = 2;

    private static final int DEFAULT_ROCKER_RADIUS = 50;

    private static final int DEFAULT_SIZE = 400;

    private static final int ROCKER_BACKGROUND_MODE_COLOR = 5;

    private static final int ROCKER_BACKGROUND_MODE_DEFAULT = 7;

    private static final int ROCKER_BACKGROUND_MODE_PIC = 4;

    private static final int ROCKER_BACKGROUND_MODE_XML = 6;

    private static final String TAG = "RockerView";

    private int mAreaBackgroundMode = 3;

    private Paint mAreaBackgroundPaint;

    private Bitmap mAreaBitmap;

    private int mAreaColor;

    public int mAreaRadius;

    private CallBackMode mCallBackMode = CallBackMode.CALL_BACK_MODE_MOVE;

    public Point mCenterPoint;

    private DirectionMode mDirectionMode;

    private OnAngleChangeListener mOnAngleChangeListener;

    private OnShakeListener mOnShakeListener;

    private int mRockerBackgroundMode = 7;

    private Bitmap mRockerBitmap;

    private int mRockerColor;

    private Paint mRockerPaint;

    public Point mRockerPosition;

    public int mRockerRadius;

    public int mRockerType = 0;

    private Direction tempDirection = Direction.DIRECTION_CENTER;

    public RockerView(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);

        initAttribute(paramContext, paramAttributeSet);
        isInEditMode();
        Paint paint = new Paint();
        this.mAreaBackgroundPaint = paint;
        paint.setAntiAlias(true);
        paint = new Paint();
        this.mRockerPaint = paint;
        paint.setAntiAlias(true);
        this.mCenterPoint = new Point();
        this.mRockerPosition = new Point();
    }

    private void callBack(double angle) {
        if (null != mOnAngleChangeListener) {
            mOnAngleChangeListener.angle(angle);
        }

        if (null != mOnShakeListener) {
            if (CallBackMode.CALL_BACK_MODE_MOVE == mCallBackMode) {
                callBackModeMove(angle);
            } else if (CallBackMode.CALL_BACK_MODE_STATE_CHANGE == mCallBackMode) {
                callBackModeStateChange(angle);
            }
        }
    }

    private void callBackModeMove(double angle) {
        switch (mDirectionMode) {
            case DIRECTION_2_HORIZONTAL:    // 左右方向
                if (ANGLE_0 <= angle && ANGLE_HORIZONTAL_2D_OF_0P > angle || ANGLE_HORIZONTAL_2D_OF_1P <= angle && ANGLE_360 > angle) {
                    // 右
                    mOnShakeListener.direction(Direction.DIRECTION_RIGHT);
                } else if (ANGLE_HORIZONTAL_2D_OF_0P <= angle && ANGLE_HORIZONTAL_2D_OF_1P > angle) {
                    // 左
                    mOnShakeListener.direction(Direction.DIRECTION_LEFT);
                }
                break;
            case DIRECTION_2_VERTICAL:// 上下方向
                if (ANGLE_VERTICAL_2D_OF_0P <= angle && ANGLE_VERTICAL_2D_OF_1P > angle) {
                    // 下
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN);
                } else if (ANGLE_VERTICAL_2D_OF_1P <= angle && ANGLE_360 > angle) {
                    // 上
                    mOnShakeListener.direction(Direction.DIRECTION_UP);
                }
                break;
            case DIRECTION_4_ROTATE_0:// 四个方向
                if (ANGLE_4D_OF_0P <= angle && ANGLE_4D_OF_1P > angle) {
                    // 右下
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN_RIGHT);
                } else if (ANGLE_4D_OF_1P <= angle && ANGLE_4D_OF_2P > angle) {
                    // 左下
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN_LEFT);
                } else if (ANGLE_4D_OF_2P <= angle && ANGLE_4D_OF_3P > angle) {
                    // 左上
                    mOnShakeListener.direction(Direction.DIRECTION_UP_LEFT);
                } else if (ANGLE_4D_OF_3P <= angle && ANGLE_360 > angle) {
                    // 右上
                    mOnShakeListener.direction(Direction.DIRECTION_UP_RIGHT);
                }
                break;
            case DIRECTION_4_ROTATE_45:// 四个方向 旋转45度
                if (ANGLE_0 <= angle && ANGLE_ROTATE45_4D_OF_0P > angle || ANGLE_ROTATE45_4D_OF_3P <= angle && ANGLE_360 > angle) {
                    // 右
                    mOnShakeListener.direction(Direction.DIRECTION_RIGHT);
                } else if (ANGLE_ROTATE45_4D_OF_0P <= angle && ANGLE_ROTATE45_4D_OF_1P > angle) {
                    // 下
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN);
                } else if (ANGLE_ROTATE45_4D_OF_1P <= angle && ANGLE_ROTATE45_4D_OF_2P > angle) {
                    // 左
                    mOnShakeListener.direction(Direction.DIRECTION_LEFT);
                } else if (ANGLE_ROTATE45_4D_OF_2P <= angle && ANGLE_ROTATE45_4D_OF_3P > angle) {
                    // 上
                    mOnShakeListener.direction(Direction.DIRECTION_UP);
                }
                break;
            case DIRECTION_8:// 八个方向
                if (ANGLE_0 <= angle && ANGLE_8D_OF_0P > angle || ANGLE_8D_OF_7P <= angle && ANGLE_360 > angle) {
                    // 右
                    mOnShakeListener.direction(Direction.DIRECTION_RIGHT);
                } else if (ANGLE_8D_OF_0P <= angle && ANGLE_8D_OF_1P > angle) {
                    // 右下
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN_RIGHT);
                } else if (ANGLE_8D_OF_1P <= angle && ANGLE_8D_OF_2P > angle) {
                    // 下
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN);
                } else if (ANGLE_8D_OF_2P <= angle && ANGLE_8D_OF_3P > angle) {
                    // 左下
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN_LEFT);
                } else if (ANGLE_8D_OF_3P <= angle && ANGLE_8D_OF_4P > angle) {
                    // 左
                    mOnShakeListener.direction(Direction.DIRECTION_LEFT);
                } else if (ANGLE_8D_OF_4P <= angle && ANGLE_8D_OF_5P > angle) {
                    // 左上
                    mOnShakeListener.direction(Direction.DIRECTION_UP_LEFT);
                } else if (ANGLE_8D_OF_5P <= angle && ANGLE_8D_OF_6P > angle) {
                    // 上
                    mOnShakeListener.direction(Direction.DIRECTION_UP);
                } else if (ANGLE_8D_OF_6P <= angle && ANGLE_8D_OF_7P > angle) {
                    // 右上
                    mOnShakeListener.direction(Direction.DIRECTION_UP_RIGHT);
                }
                break;
            default:
                break;
        }
    }

    private void callBackModeStateChange(double angle) {
        switch (mDirectionMode) {
            case DIRECTION_2_HORIZONTAL:// 左右方向
                if ((ANGLE_0 <= angle && ANGLE_HORIZONTAL_2D_OF_0P > angle || ANGLE_HORIZONTAL_2D_OF_1P <= angle && ANGLE_360 > angle) && tempDirection != Direction.DIRECTION_RIGHT) {
                    // 右
                    tempDirection = Direction.DIRECTION_RIGHT;
                    mOnShakeListener.direction(Direction.DIRECTION_RIGHT);
                } else if (ANGLE_HORIZONTAL_2D_OF_0P <= angle && ANGLE_HORIZONTAL_2D_OF_1P > angle && tempDirection != Direction.DIRECTION_LEFT) {
                    // 左
                    tempDirection = Direction.DIRECTION_LEFT;
                    mOnShakeListener.direction(Direction.DIRECTION_LEFT);
                }
                break;
            case DIRECTION_2_VERTICAL:// 上下方向
                if (ANGLE_VERTICAL_2D_OF_0P <= angle && ANGLE_VERTICAL_2D_OF_1P > angle && tempDirection != Direction.DIRECTION_DOWN) {
                    // 下
                    tempDirection = Direction.DIRECTION_DOWN;
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN);
                } else if (ANGLE_VERTICAL_2D_OF_1P <= angle && ANGLE_360 > angle && tempDirection != Direction.DIRECTION_UP) {
                    // 上
                    tempDirection = Direction.DIRECTION_UP;
                    mOnShakeListener.direction(Direction.DIRECTION_UP);
                }
                break;
            case DIRECTION_4_ROTATE_0:// 四个方向
                if (ANGLE_4D_OF_0P <= angle && ANGLE_4D_OF_1P > angle && tempDirection != Direction.DIRECTION_DOWN_RIGHT) {
                    // 右下
                    tempDirection = Direction.DIRECTION_DOWN_RIGHT;
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN_RIGHT);
                } else if (ANGLE_4D_OF_1P <= angle && ANGLE_4D_OF_2P > angle && tempDirection != Direction.DIRECTION_DOWN_LEFT) {
                    // 左下
                    tempDirection = Direction.DIRECTION_DOWN_LEFT;
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN_LEFT);
                } else if (ANGLE_4D_OF_2P <= angle && ANGLE_4D_OF_3P > angle && tempDirection != Direction.DIRECTION_UP_LEFT) {
                    // 左上
                    tempDirection = Direction.DIRECTION_UP_LEFT;
                    mOnShakeListener.direction(Direction.DIRECTION_UP_LEFT);
                } else if (ANGLE_4D_OF_3P <= angle && ANGLE_360 > angle && tempDirection != Direction.DIRECTION_UP_RIGHT) {
                    // 右上
                    tempDirection = Direction.DIRECTION_UP_RIGHT;
                    mOnShakeListener.direction(Direction.DIRECTION_UP_RIGHT);
                }
                break;
            case DIRECTION_4_ROTATE_45:// 四个方向 旋转45度
                if ((ANGLE_0 <= angle && ANGLE_ROTATE45_4D_OF_0P > angle || ANGLE_ROTATE45_4D_OF_3P <= angle && ANGLE_360 > angle) && tempDirection != Direction.DIRECTION_RIGHT) {
                    // 右
                    tempDirection = Direction.DIRECTION_RIGHT;
                    mOnShakeListener.direction(Direction.DIRECTION_RIGHT);
                } else if (ANGLE_ROTATE45_4D_OF_0P <= angle && ANGLE_ROTATE45_4D_OF_1P > angle && tempDirection != Direction.DIRECTION_DOWN) {
                    // 下
                    tempDirection = Direction.DIRECTION_DOWN;
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN);
                } else if (ANGLE_ROTATE45_4D_OF_1P <= angle && ANGLE_ROTATE45_4D_OF_2P > angle && tempDirection != Direction.DIRECTION_LEFT) {
                    // 左
                    tempDirection = Direction.DIRECTION_LEFT;
                    mOnShakeListener.direction(Direction.DIRECTION_LEFT);
                } else if (ANGLE_ROTATE45_4D_OF_2P <= angle && ANGLE_ROTATE45_4D_OF_3P > angle && tempDirection != Direction.DIRECTION_UP) {
                    // 上
                    tempDirection = Direction.DIRECTION_UP;
                    mOnShakeListener.direction(Direction.DIRECTION_UP);
                }
                break;
            case DIRECTION_8:// 八个方向
                if ((ANGLE_0 <= angle && ANGLE_8D_OF_0P > angle || ANGLE_8D_OF_7P <= angle && ANGLE_360 > angle) && tempDirection != Direction.DIRECTION_RIGHT) {
                    // 右
                    tempDirection = Direction.DIRECTION_RIGHT;
                    mOnShakeListener.direction(Direction.DIRECTION_RIGHT);
                } else if (ANGLE_8D_OF_0P <= angle && ANGLE_8D_OF_1P > angle && tempDirection != Direction.DIRECTION_DOWN_RIGHT) {
                    // 右下
                    tempDirection = Direction.DIRECTION_DOWN_RIGHT;
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN_RIGHT);
                } else if (ANGLE_8D_OF_1P <= angle && ANGLE_8D_OF_2P > angle && tempDirection != Direction.DIRECTION_DOWN) {
                    // 下
                    tempDirection = Direction.DIRECTION_DOWN;
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN);
                } else if (ANGLE_8D_OF_2P <= angle && ANGLE_8D_OF_3P > angle && tempDirection != Direction.DIRECTION_DOWN_LEFT) {
                    // 左下
                    tempDirection = Direction.DIRECTION_DOWN_LEFT;
                    mOnShakeListener.direction(Direction.DIRECTION_DOWN_LEFT);
                } else if (ANGLE_8D_OF_3P <= angle && ANGLE_8D_OF_4P > angle && tempDirection != Direction.DIRECTION_LEFT) {
                    // 左
                    tempDirection = Direction.DIRECTION_LEFT;
                    mOnShakeListener.direction(Direction.DIRECTION_LEFT);
                } else if (ANGLE_8D_OF_4P <= angle && ANGLE_8D_OF_5P > angle && tempDirection != Direction.DIRECTION_UP_LEFT) {
                    // 左上
                    tempDirection = Direction.DIRECTION_UP_LEFT;
                    mOnShakeListener.direction(Direction.DIRECTION_UP_LEFT);
                } else if (ANGLE_8D_OF_5P <= angle && ANGLE_8D_OF_6P > angle && tempDirection != Direction.DIRECTION_UP) {
                    // 上
                    tempDirection = Direction.DIRECTION_UP;
                    mOnShakeListener.direction(Direction.DIRECTION_UP);
                } else if (ANGLE_8D_OF_6P <= angle && ANGLE_8D_OF_7P > angle && tempDirection != Direction.DIRECTION_UP_RIGHT) {
                    // 右上
                    tempDirection = Direction.DIRECTION_UP_RIGHT;
                    mOnShakeListener.direction(Direction.DIRECTION_UP_RIGHT);
                }
                break;
            default:
                break;
        }

    }

    private Bitmap drawable2Bitmap(Drawable paramDrawable) {
        Bitmap.Config config;
        int i = paramDrawable.getIntrinsicWidth();
        int j = paramDrawable.getIntrinsicHeight();

        if (paramDrawable.getOpacity() != PixelFormat.OPAQUE) {
            config = Bitmap.Config.ARGB_8888;
        } else {
            config = Bitmap.Config.RGB_565;
        }
        Bitmap bitmap = Bitmap.createBitmap(i, j, config);
        Canvas canvas = new Canvas(bitmap);
        paramDrawable.setBounds(0, 0, i, j);
        paramDrawable.draw(canvas);
        return bitmap;
    }

    private void initAttribute(Context paramContext, AttributeSet paramAttributeSet) {
        TypedArray typedArray = paramContext.obtainStyledAttributes(paramAttributeSet, R.styleable.RockerView);
        Drawable drawable = typedArray.getDrawable(R.styleable.RockerView_areaBackground);
        if (drawable != null) {
            if (drawable instanceof BitmapDrawable) {
                this.mAreaBitmap = ((BitmapDrawable) drawable).getBitmap();
                this.mAreaBackgroundMode = 0;
            } else if (drawable instanceof android.graphics.drawable.GradientDrawable) {
                this.mAreaBitmap = drawable2Bitmap(drawable);
                this.mAreaBackgroundMode = 2;
            } else if (drawable instanceof ColorDrawable) {
                this.mAreaColor = ((ColorDrawable) drawable).getColor();
                this.mAreaBackgroundMode = 1;
            } else {
                this.mAreaBackgroundMode = 3;
            }
        } else {
            this.mAreaBackgroundMode = 3;
        }
        drawable = typedArray.getDrawable(R.styleable.RockerView_rockerBackground);
        if (drawable != null) {
            if (drawable instanceof BitmapDrawable) {
                this.mRockerBitmap = ((BitmapDrawable) drawable).getBitmap();
                this.mRockerBackgroundMode = 4;
            } else if (drawable instanceof android.graphics.drawable.GradientDrawable) {
                this.mRockerBitmap = drawable2Bitmap(drawable);
                this.mRockerBackgroundMode = 6;
            } else if (drawable instanceof ColorDrawable) {
                this.mRockerColor = ((ColorDrawable) drawable).getColor();
                this.mRockerBackgroundMode = 5;
            } else {
                this.mRockerBackgroundMode = 7;
            }
        } else {
            this.mRockerBackgroundMode = 7;
        }
        this.mRockerType = typedArray.getInteger(R.styleable.RockerView_rockerType, 0);
        this.mRockerRadius = typedArray.getDimensionPixelOffset(R.styleable.RockerView_rockerRadius, 50);
        typedArray.recycle();
    }

    private double radian2Angle(double paramDouble) {
        paramDouble = Math.round(paramDouble / Math.PI * 180.0D);
        return (paramDouble >= 0.0D) ? paramDouble : (paramDouble + 360.0D);
    }

    public void callBackFinish() {
        this.tempDirection = Direction.DIRECTION_CENTER;
        OnAngleChangeListener onAngleChangeListener = this.mOnAngleChangeListener;
        if (onAngleChangeListener != null) {
            onAngleChangeListener.onFinish();
        }
        OnShakeListener onShakeListener = this.mOnShakeListener;
        if (onShakeListener != null) {
            onShakeListener.onFinish();
        }
    }

    public void callBackStart() {
        this.tempDirection = Direction.DIRECTION_CENTER;
        OnAngleChangeListener onAngleChangeListener = this.mOnAngleChangeListener;
        if (onAngleChangeListener != null) {
            onAngleChangeListener.onStart();
        }
        OnShakeListener onShakeListener = this.mOnShakeListener;
        if (onShakeListener != null) {
            onShakeListener.onStart();
        }
    }

    public void doDwn(float paramFloat1, float paramFloat2) {
        this.mRockerPosition = getRockerPositionPoint(this.mCenterPoint, new Point((int) paramFloat1, (int) paramFloat2), this.mAreaRadius, this.mRockerRadius);
        callBackStart();
    }

    public void doMove(float paramFloat1, float paramFloat2) {
        Point point = getRockerPositionPoint(this.mCenterPoint, new Point((int) paramFloat1, (int) paramFloat2), this.mAreaRadius, this.mRockerRadius);
        this.mRockerPosition = point;
        moveRocker(point.x, this.mRockerPosition.y);
    }

    public void doUp() {
        callBackFinish();
        moveRocker(this.mCenterPoint.x, this.mCenterPoint.y);
    }

    private static final double MAX_SPEED            = 0.5;//最大0.8
    private static final double MAX_ANGULAR_VELOCITY = 2;//最大5.4


    public Point getRockerPositionPoint(Point centerPoint, Point touchPoint, float regionRadius, float rockerRadius) {

        // 两点在X轴的距离
        float lenX = (float) (touchPoint.x - centerPoint.x);
        // 两点在Y轴距离
        float lenY = (float) (touchPoint.y - centerPoint.y);
        // 两点距离
        float lenXY = (float) Math.sqrt(lenX * lenX + lenY * lenY);
        // 计算弧度
        double radian = Math.acos(lenX / lenXY) * (touchPoint.y < centerPoint.y ? -1 : 1);
        // 计算角度
        double angle = radian2Angle(radian);

        Log.i("gzw", "angle: " + angle);

        // 回调 返回参数
        callBack(angle);

        if (lenXY + rockerRadius <= regionRadius) { // 触摸位置在可活动范围内
            return touchPoint;
        } else { // 触摸位置在可活动范围以外
            // 计算要显示的位置
            int showPointX = (int) (centerPoint.x + (regionRadius - rockerRadius) * Math.cos(radian));
            int showPointY = (int) (centerPoint.y + (regionRadius - rockerRadius) * Math.sin(radian));
            return new Point(showPointX, showPointY);
        }
    }

    public int getRockerType() {
        return this.mRockerType;
    }

    public void moveRocker(float paramFloat1, float paramFloat2) {
        this.mRockerPosition.set((int) paramFloat1, (int) paramFloat2);
        invalidate();
    }

    private Rect mRect1;
    private Rect mRect2;

    @Override
    protected void onDraw(Canvas paramCanvas) {
        super.onDraw(paramCanvas);
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();

        int cx = measuredWidth / 2;
        int cy = measuredHeight / 2;

        // 中心点
        mCenterPoint.set(cx, cy);

        // 可移动区域的半径
        mAreaRadius = (measuredWidth <= measuredHeight) ? cx : cy;

        if (this.mRockerPosition.x == 0 || this.mRockerPosition.y == 0) {
            this.mRockerPosition.set(this.mCenterPoint.x, this.mCenterPoint.y);
        }
        int areaBackgroundMode = this.mAreaBackgroundMode;

        switch (areaBackgroundMode) {
            case 0:
            case 2:
                if (mRect1 == null) {
                    mRect1 = new Rect(0, 0, this.mAreaBitmap.getWidth(), this.mAreaBitmap.getHeight());
                } else {
                    mRect1.right = this.mAreaBitmap.getWidth();
                    mRect1.bottom = this.mAreaBitmap.getHeight();
                }

                if (mRect2 == null) {
                    mRect2 = new Rect(this.mCenterPoint.x - this.mAreaRadius, this.mCenterPoint.y - this.mAreaRadius, this.mCenterPoint.x + this.mAreaRadius, this.mCenterPoint.y + this.mAreaRadius);
                } else {
                    mRect2.left = this.mCenterPoint.x - this.mAreaRadius;
                    mRect2.top = this.mCenterPoint.y - this.mAreaRadius;
                    mRect2.right = this.mCenterPoint.x + this.mAreaRadius;
                    mRect2.bottom = this.mCenterPoint.y + this.mAreaRadius;
                }

                paramCanvas.drawBitmap(this.mAreaBitmap, mRect1, mRect2, this.mAreaBackgroundPaint);
                break;
            case 1:
                this.mAreaBackgroundPaint.setColor(this.mAreaColor);
                paramCanvas.drawCircle(this.mCenterPoint.x, this.mCenterPoint.y, this.mAreaRadius, this.mAreaBackgroundPaint);
                break;
            default:
                this.mAreaBackgroundPaint.setColor(ContextCompat.getColor(getContext(), R.color.color_gray));
                paramCanvas.drawCircle(this.mCenterPoint.x, this.mCenterPoint.y, this.mAreaRadius, this.mAreaBackgroundPaint);
                break;
        }

        int rockerBackgroundMode = this.mRockerBackgroundMode;
        switch (rockerBackgroundMode) {
            case 4:
            case 6:

                if (mRect1 == null) {
                    mRect1 = new Rect(0, 0, this.mRockerBitmap.getWidth(), this.mRockerBitmap.getHeight());
                } else {
                    mRect1.right = this.mRockerBitmap.getWidth();
                    mRect1.bottom = this.mRockerBitmap.getHeight();
                }

                mRect2 = new Rect(this.mRockerPosition.x - this.mRockerRadius, this.mRockerPosition.y - this.mRockerRadius, this.mRockerPosition.x + this.mRockerRadius, this.mRockerPosition.y + this.mRockerRadius);
                paramCanvas.drawBitmap(this.mRockerBitmap, mRect1, mRect2, this.mRockerPaint);
                break;
            case 5:
                this.mRockerPaint.setColor(this.mRockerColor);
                paramCanvas.drawCircle(this.mRockerPosition.x, this.mRockerPosition.y, this.mRockerRadius, this.mRockerPaint);
                break;
            default:
                this.mRockerPaint.setColor(ContextCompat.getColor(getContext(), R.color.color_f00));
                paramCanvas.drawCircle(this.mRockerPosition.x, this.mRockerPosition.y, this.mRockerRadius, this.mRockerPaint);
                break;
        }
    }

    @Override
    protected void onMeasure(int paramInt1, int paramInt2) {
        int j = View.MeasureSpec.getMode(paramInt1);
        int i = View.MeasureSpec.getMode(paramInt2);
        paramInt1 = View.MeasureSpec.getSize(paramInt1);
        paramInt2 = View.MeasureSpec.getSize(paramInt2);

        if (j != MeasureSpec.EXACTLY) {
            paramInt1 = 400;
        }
        if (i != MeasureSpec.EXACTLY) {
            paramInt2 = 400;
        }
        setMeasuredDimension(paramInt1, paramInt2);
    }

    public void setCallBackMode(CallBackMode paramCallBackMode) {
        this.mCallBackMode = paramCallBackMode;
    }

    public void setOnAngleChangeListener(OnAngleChangeListener paramOnAngleChangeListener) {
        this.mOnAngleChangeListener = paramOnAngleChangeListener;
    }

    public void setOnShakeListener(DirectionMode paramDirectionMode, OnShakeListener paramOnShakeListener) {
        this.mDirectionMode = paramDirectionMode;
        this.mOnShakeListener = paramOnShakeListener;
    }

    public void setRockBackIn(Bitmap paramBitmap) {
        this.mRockerBitmap = paramBitmap;
        invalidate();
    }

    public void setRockBackOut(Bitmap paramBitmap) {
        this.mAreaBitmap = paramBitmap;
        invalidate();
    }

    public void setRockerRadiusBySize(int paramInt) {
        this.mRockerRadius = (int) ((paramInt - 5) * 0.1D * 50.0D + 50.0D);
        requestLayout();
        invalidate();
    }

    public void setRockerType(int paramInt) {
        this.mRockerType = paramInt;
    }


    public static interface OnAngleChangeListener {
        void angle(double param1Double);

        void onFinish();

        void onStart();
    }

    public static interface OnShakeListener {
        void direction(Direction param1Direction);

        void onFinish();

        void onStart();
    }
}